<template>
    <el-card class="feature-submission-card">
      <el-form :model="form" :rules="rules" ref="featureForm" @submit.native.prevent="submitFeature">
        <el-form-item label="添加特色" prop="feature">
          <el-input
            v-model="form.feature"
            placeholder="请输入餐厅的特色..."
            maxlength="50"
          />
        </el-form-item>
        <el-form-item>
          <el-button :loading="isSubmitting" type="primary" @click="submitFeature">提交特色</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </template>
  
  <script setup lang="ts">
  import { ref,defineProps } from 'vue';
  import { ElCard, ElForm, ElFormItem, ElInput, ElButton, ElMessage } from 'element-plus';
  import axios from 'axios';
  
  const props = defineProps({
    restaurantId: String,
  })
  interface FeatureForm {
    feature: string;
  }
  
  const form = ref<FeatureForm>({
    feature: ''
  });
  
  const rules = {
    feature: [
      { required: true, message: '请输入餐厅的特色', trigger: 'blur' },
      { min: 2, message: '特色内容至少需要 2 个字符', trigger: 'blur' }
    ]
  };
  
  const featureForm = ref();
  const isSubmitting = ref(false);
  
  const submitFeature = async () => {
    featureForm.value?.validate(async (valid: boolean) => {
      if (valid) {
        isSubmitting.value = true;
        try {
          const response = await axios.post(`/api/restaurants/${props.restaurantId}/features`, form.value);
          if (response.data.code === 200) {
            ElMessage.success('特色提交成功');
            resetForm();
          } else {
            ElMessage.error(response.data.message || '特色提交失败');
          }
        } catch (error) {
          console.error('提交特色时出错:', error);
          ElMessage.error('特色提交失败，请重试');
        } finally {
          isSubmitting.value = false;
        }
      } else {
        ElMessage.error('请填写完整的特色信息');
      }
    });
  };
  
  const resetForm = () => {
    form.value.feature = '';
  };
  </script>
  
  <style scoped>
  .feature-submission-card {
    margin-bottom: 20px;
    padding: 20px;
    background-color: #f5f5f5;
    border-radius: 10px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  }
  </style>
  